feat(component-meta): extend component meta#5169
Conversation
| if (!ts.isPropertyAssignment(property)) return | ||
| const key = property.name.getText(); | ||
| const valueNode = resolveDefaultOptionExpression(property.initializer, ts); | ||
| meta[key] = printer?.printNode(ts.EmitHint.Expression, valueNode, ast) ?? valueNode.getText(ast) |
There was a problem hiding this comment.
@KazariEX I'm wondering if it's a good idea to parse nested objects and arrays by traversing the rest of the AST here. I tried to match the parser for prop default values.
On one side it would be nice for end users to receive plain objects / arrays, but might be a bit heavy on performances.
| }); | ||
| } | ||
| } | ||
| return ts.forEachChild(node, traverse) |
There was a problem hiding this comment.
This is a bit aggressive since it'll walk the entire AST. I'll look into restraining it to the top level of <script setup> and defineComponent.
There was a problem hiding this comment.
I'm not sure if we can parse defineComponentMeta at language-core so that we can read the node directly without repeat traversal, since it's not useful for tsc and IDE.
| let meta: Record<string, any> = {}; | ||
| const snapshot = language.scripts.get(componentPath)?.snapshot!; | ||
| const fileText = snapshot.getText(0, snapshot.getLength()); | ||
| const ast = ts.createSourceFile('/temp', fileText, ts.ScriptTarget.Latest, true); |
There was a problem hiding this comment.
See:
language-tools/packages/component-meta/lib/base.ts
Lines 334 to 344 in 317b0cc
We can read ast from sourceFile.generated.root._sfc if it's a .vue file, or try sharing parsed ast if it's a .ts file.
Resolves #5168
Introduces a new macro
defineComponentMetato extend component meta with arbitrary values.Results in the following meta: